package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.Post;
import com.fz.us.admin.company.dao.PostDao;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Dao实现类 - 岗位
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-27 。
 * ============================================================================
 */
@Repository
public class PostDaoImpl extends BaseDaoImpl<Post, String> implements PostDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Post> getAllPosts(Company company) {
        Assert.notNull(company, "company is required");
        String hql = "from " + Post.class.getName() + " model where model.company = :company ";
        return getSession().createQuery(hql).setParameter("company", company).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Post> getAllPosts(Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");
        Assert.notEmpty(states, "states is required");
        String hql = "from " + Post.class.getName() + " model where model.company = :company and model.state in :states ";
        return getSession().createQuery(hql).setParameter("company", company).setParameterList("states", states).list();
    }

    @Override
    public Post getPostByName(String name) {
        Assert.notNull(name, "name is required");
        String hql = "from" + Post.class.getName() + "  p where p.name=? and p.state = ?";
        List<Post> postList= (List<Post>) getSession().createQuery(hql).setParameter(0,name).setParameter(1, BaseEnumManager.StateEnum.Enable).list();
        if(postList.size()>0){
            return postList.get(0);
        }else {
            return null;
        }
    }


}